/**
 *
 * @ Moises.Perez.Espinosa
 */
package commonlib.domain;

import java.util.*;

public class Text
{
    // VARIABLES: --------------------------------------------------------------

    private ArrayList<TxtWord> words;

    // CONSTRUCTORA: -----------------------------------------------------------

    public Text (String text)
    {
        String[] foo = text.split( " " );
        extractCaracters(foo);
        words = new ArrayList<TxtWord>();
        for (int i = 0; i < foo.length; i++) this.words.add( new TxtWord(foo[i]));
    }

    public Text ()
    {
        this.words = new ArrayList<TxtWord>();
    }

    private void extractCaracters(String[] foo)
    {
        for (int i = 0; i < foo.length; ++i)
        {
            foo[i].replaceAll(","  , "");
            foo[i].replaceAll(":"  , "");
            foo[i].replaceAll(";"  , "");
            foo[i].replaceAll("\\?", "");
            foo[i].replaceAll("¿"  , "");
            foo[i].replaceAll("!"  , "");
            foo[i].trim();
        }
    }

    // GETTERS: ----------------------------------------------------------------

    public TxtWord getWord(int posicion)
    {
        return words.get(posicion);
    }


    // SETTERS: ----------------------------------------------------------------

    public void addWord(String word)
    {
        words.add(new TxtWord(word));
    }

    public void addWord(String word, int posicio)
    {
        words.add(posicio, new TxtWord(word));
    }

    public void setWord (int posicion, String newWord)
    {
        words.set(posicion, new TxtWord (newWord));
    }

    public void markIncorrect(Dictionary diccio)
    {
        for (int i  = 0; i < words.size(); ++i)
        {
            if(diccio.findWord(words.get(i).getWord()))
            {
                words.get(i).setCorrect();
            }
        }
    }

    public boolean delete(TxtWord word, int posicion)
    {
        if(words.contains(word))
        {
            words.remove(posicion);
            return true;
        }
        else return false;
    }

    public void setCorrected(int posicion) {
        words.get(posicion).setCorrected();
    }

    public void setCorrect(int posicion)
    {
        words.get(posicion).setCorrect();
    }


    public boolean isCorrected(int position) {
        return words.get(position).isCorrected();
    }

    public boolean getCorrect(int posicion)
    {
        return words.get(posicion).getCorrect();
    }

    public int getsize()
    {
        return words.size();
    }
}